package com.training.admin.web.controller.report;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.training.admin.entity.department.Department;
import com.training.admin.entity.principal.Principal;
import com.training.admin.entity.search.ProjectCourseReportSearch;
import com.training.admin.entity.security.Login;
import com.training.admin.service.DepartmentService;
import com.training.admin.service.PrincipalService;
import com.training.admin.service.ReportService;
import com.training.admin.service.TrainingCategoryService;
import com.training.admin.web.controller.view.ExcelTemplateView;
/**
 * 
 * @author sjh
 *
 */
@Controller
@RequestMapping("")
@MenuSetting("menu-report")
public class ProjectCourseReport {

	@Autowired
	private TrainingCategoryService trainingCategoryService;

	@Autowired
	private PrincipalService principalService;
	
	@Autowired
	private DepartmentService departmentService;

	@Autowired
	private ReportService reportService;


	/**
	 * 项目课程报表查询条件输入
	 * 
	 * @param page
	 * @return
	 */
	@SuppressWarnings("rawtypes")
    @RequestMapping(value = "/project-course-report", method = RequestMethod.GET)
	@MenuSetting("submenu-project-course-report")
	public ModelAndView projectCourseReportSearch(Page page) {
		ModelAndView modelAndView = new ModelAndView("report/project-course-report-search");
		// 培训分类
		Map catMap = trainingCategoryService.getAllCategory();
		Map allCatMap = (Map) catMap.get("allCatMap");
		Map allNameIdMap = (Map) catMap.get("allNameIdMap");

		modelAndView.addObject("allCatMap", allCatMap);
		modelAndView.addObject("allNameIdMap", allNameIdMap);
		// 事业部
		List<Department> depList = new ArrayList<Department>();
		Login login = (Login) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Principal principal = principalService.loadByEmail(login.getEmail());
		if (principal.getAuthority().equals("ROLE_SUPER_USER")) {
			// 系统管理员
			depList = departmentService.listAll(Department.class);
		} else if (principal.getAuthority().equals("ROLE_DEPARTMENT_USER")) {
			// 事业部负责人
			depList.add(departmentService.load(Department.class, principal.getDepartmentId()));
		}
		modelAndView.addObject("departmentList", depList);

		return modelAndView;
	}

	/**
	 * 项目课程报表
	 * 
	 * @param page
	 * @return
	 */
	@SuppressWarnings("rawtypes")
    @RequestMapping(value = "/project-course-report", method = RequestMethod.GET, params = "filter")
	@MenuSetting("submenu-project-course-report")
	public ModelAndView onProjectCourseReportSearch(ProjectCourseReportSearch projectCourseReportSearch, Page page,
			HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("report/project-course-report");

		Login login = (Login) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Principal principal = principalService.loadByEmail(login.getEmail());
		if (principal.getAuthority().equals("ROLE_SUPER_USER")) {
			// 系统管理员
			// do nothing
		} else if (principal.getAuthority().equals("ROLE_DEPARTMENT_USER")) {
			// 事业部负责人
			projectCourseReportSearch.setDepartmentId(principal.getDepartmentId());
		}

		int totals = reportService.count(projectCourseReportSearch);
		if (totals > 0) {
			// List<ProjectPhaseCourse> projectCourseReports = new
			// ArrayList<ProjectPhaseCourse>();
			List reportList = reportService.search(projectCourseReportSearch, page.with(totals));

			modelAndView.addObject("projectReports", reportList);
			modelAndView.addObject("recordCount", reportList.size());
			modelAndView.addObject("page", page);

		}

		modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));

		return modelAndView;
	}
	
	/**
	 * 项目课程报表 - 导出
	 * 
	 * @param search
	 * @return
	 */
	@SuppressWarnings("rawtypes")
    @RequestMapping(value = "/reports/project-course-report-export", method = RequestMethod.GET)
	public ModelAndView onProjectReportExport(ProjectCourseReportSearch projectCourseReportSearch, Page page) {

		ModelAndView modelAndView = new ModelAndView(
				new ExcelTemplateView("project-course-report.xls", "project-course-report.xls"));

		Login login = (Login) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Principal principal = principalService.loadByEmail(login.getEmail());
		if (principal.getAuthority().equals("ROLE_SUPER_USER")) {
			// 系统管理员
			// do nothing
		} else if (principal.getAuthority().equals("ROLE_DEPARTMENT_USER")) {
			// 事业部负责人
			projectCourseReportSearch.setDepartmentId(principal.getDepartmentId());
		}

		int totals = reportService.count(projectCourseReportSearch);
		if (totals > 0) {

			List reportList = reportService.searchForExport(projectCourseReportSearch);

			modelAndView.addObject("results", reportList);

		}

		return modelAndView;

	}
}
